package com.dajie.pt.service;

import com.dajie.pt.service.utils.CommonUtil;
import com.dajie.pt.service.utils.SendSms;
import com.dajie.pt.service.utils.StringUtil;
import com.dajie.pt.service.utils.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yunbinan on 15-7-13.
 */
public class UserFeedJobForHr {
    private static final int BAILING = 10001;
    private static final int XUESHENG = 10002;
    private static final int OTHER = 10003;
    public static final int TYPE = 3000;
    private static Logger logger = LoggerFactory.getLogger(UserFeedJobForHr.class);
    private Connection bssConnection = null;
    private static SendSms sendSms = new SendSms();

    //case class GuankeUserFeedJobPO(hrId: Int, jobCount: Int, jobId: String, userCount: Int, undealCount: Int)
    //case class HrJobFeedUserPO(hrId: Int, jobIds: String, feedCount: String)
    class UserFeedJobPO {
        private int hrId;   //HR   ID
        private int userId; //用户 ID
        private int feedCount; //感兴趣人

        public int getHrId() {
            return hrId;
        }

        public void setHrId(int hrId) {
            this.hrId = hrId;
        }

        public int getUserId() {
            return userId;
        }

        public void setUserId(int userId) {
            this.userId = userId;
        }

        public int getFeedCount() {
            return feedCount;
        }

        public void setFeedCount(int feedCount) {
            this.feedCount = feedCount;
        }

        public UserFeedJobPO(int hrId, int userId, int feedCount) {
            this.hrId = hrId;
            this.userId = userId;
            this.feedCount = feedCount;
        }
    }

    public List<UserFeedJobPO> getUserFeedJob() {
        List<UserFeedJobPO> userFeedJobPOs = new ArrayList();
        String sql = "SELECT hr_id, MAX(user_id), COUNT(DISTINCT user_id)\n" +
                "FROM tb_guanke_user_job\n" +
                "WHERE hr_id != 0\n" +
                "\tAND status = 0\n" +
                "GROUP BY hr_id;";
        try {
            java.sql.Statement statement = bssConnection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                UserFeedJobPO dashanData = new UserFeedJobPO(rs.getInt(1), rs.getInt(2), rs.getInt(3));
                userFeedJobPOs.add(dashanData);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userFeedJobPOs;
    }

    public UserFeedJobForHr() {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:cobar://cobarvip:12001/DB_BSS?autoCommit=true&autoReconnect=true&useUnicode=true&tinyInt1isBit=false&zeroDateTimeBehavior=round&characterEncoding=UTF-8&yearIsDateType=false&zeroDateTimeBehavior=convertToNull";
        String user = "platformol";
        String password = "platformol-3";
        try {
            Class.forName(driver);
            bssConnection = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void closeConn() {
        try {
            bssConnection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        UserFeedJobForHr userFeedJobForHr = new UserFeedJobForHr();
        List<UserFeedJobPO> userFeedJobList = userFeedJobForHr.getUserFeedJob();
        logger.error("main --> userFeedJobList size:{}", userFeedJobList.size());
        Map<String, String> map = new HashMap<String, String>();
        for (UserFeedJobPO userFeedJobPO : userFeedJobList) {
            try {
                String uName = "";
                String hrName = "";
                String uShcoolName = "";
                String uMajorName = "";
                String uCorpName = "";
                int workYear = 0;
                String hrMobile = "";
                String result = "";
                map.clear();
                int identity = CommonUtil.getIdentityByUid(userFeedJobPO.getUserId());
                switch (identity) {
                    //白领
                    case 1:
                        hrName = CommonUtil.getUnameByUid(userFeedJobPO.getHrId());
                        uName = CommonUtil.getUnameByUid(userFeedJobPO.getUserId());
                        uCorpName = CommonUtil.getCorpNameByUid(userFeedJobPO.getUserId());
                        hrMobile = CommonUtil.getMobileByUid(userFeedJobPO.getHrId());
                        workYear = CommonUtil.getWorkYearByUid(userFeedJobPO.getUserId());
                        map.put("hrName", hrName);
                        map.put("uName", uName);
                        map.put("uCorpName", uCorpName);
                        map.put("MOBILE", hrMobile);
                        map.put("userCount", String.valueOf(userFeedJobPO.feedCount));
                        map.put("workYear", String.valueOf(workYear));
                        map.put("UID", String.valueOf(userFeedJobPO.hrId));
                        if (!StringUtil.isEmpty(hrName) && !StringUtil.isEmpty(uName) && !StringUtil.isEmpty(uCorpName) && !StringUtil.isEmpty(hrMobile) && workYear != 0) {
                            map.put("smsUrl", CommonUtil.smsUrlForHr("gallery=" + "guodu" + "&signature=" + "dajie" + "&ch=djgkdashan" + TimeUtil.getST() + BAILING + "&st=" + TimeUtil.getST() + "&sms_uid=" + userFeedJobPO.hrId));
                            // result = SendSms.sendSms(TYPE, BAILING, map);
                            result = sendSms.sendSmsSingle(hrMobile, String.valueOf(BAILING), map);
                            if (result.contains("1")) {
                                logger.error("main --> sendSms success hrUid:{} uUid:{} hrMobile:{} tplId:{} result:{} map:{}", userFeedJobPO.hrId, userFeedJobPO.userId, hrMobile, BAILING, result, map.toString());
                            } else {
                                logger.error("main --> sendSms fail hrUid:{} identity:{} result:{} map:{}", userFeedJobPO.hrId, identity, result, map.toString());
                            }
                        } else {
                            logger.error("main --> getInfo lost hrUid:{} uUid:{} identity:{} map:{}", userFeedJobPO.hrId, userFeedJobPO.userId, identity, map.toString());
                        }

                        break;
                    //学生
                    case 3:
                        hrName = CommonUtil.getUnameByUid(userFeedJobPO.getHrId());
                        uName = CommonUtil.getUnameByUid(userFeedJobPO.getUserId());
                        uShcoolName = CommonUtil.getSchoolNameByUid(userFeedJobPO.getUserId());
                        uMajorName = CommonUtil.getMajorNameByUid(userFeedJobPO.getUserId());
                        hrMobile = CommonUtil.getMobileByUid(userFeedJobPO.getHrId());
                        map.put("hrName", hrName);
                        map.put("uName", uName);
                        map.put("uShcoolName", uShcoolName);
                        map.put("uMajorName", uMajorName);
                        map.put("userCount", String.valueOf(userFeedJobPO.feedCount));
                        map.put("MOBILE", hrMobile);
                        map.put("UID", String.valueOf(userFeedJobPO.hrId));
                        if (!StringUtil.isEmpty(hrName) && !StringUtil.isEmpty(uName) && !StringUtil.isEmpty(uShcoolName) && !StringUtil.isEmpty(hrMobile) && !StringUtil.isEmpty(uMajorName)) {
                            map.put("smsUrl", CommonUtil.smsUrlForHr("gallery=" + "guodu" + "&signature=" + "dajie" + "&ch=djgkdashan" + TimeUtil.getST() + XUESHENG + "&st=" + TimeUtil.getST() + "&sms_uid=" + userFeedJobPO.hrId));
                            // result = SendSms.sendSms(TYPE, XUESHENG, map);
                            result = sendSms.sendSmsSingle(hrMobile, String.valueOf(XUESHENG), map);
                            if (result.contains("1")) {
                                logger.error("main --> sendSms success hrUid:{} uUid:{} hrMobile:{} tplId:{} result:{} map:{}", userFeedJobPO.hrId, userFeedJobPO.userId, hrMobile, XUESHENG, result, map.toString());
                            } else {
                                logger.error("main --> sendSms fail hrUid:{} identity:{} result:{} map:{}", userFeedJobPO.hrId, identity, result, map.toString());
                            }
                        } else {
                            logger.error("main --> getInfo lost hrUid:{} uUid:{} identity:{} map:{}", userFeedJobPO.hrId, userFeedJobPO.userId, identity, map.toString());
                        }
                        break;
                    //学生
                    case 4:
                        hrName = CommonUtil.getUnameByUid(userFeedJobPO.getHrId());
                        uName = CommonUtil.getUnameByUid(userFeedJobPO.getUserId());
                        uShcoolName = CommonUtil.getSchoolNameByUid(userFeedJobPO.getUserId());
                        uMajorName = CommonUtil.getMajorNameByUid(userFeedJobPO.getUserId());
                        hrMobile = CommonUtil.getMobileByUid(userFeedJobPO.getHrId());
                        map.put("hrName", hrName);
                        map.put("uName", uName);
                        map.put("uShcoolName", uShcoolName);
                        map.put("uMajorName", uMajorName);
                        map.put("userCount", String.valueOf(userFeedJobPO.feedCount));
                        map.put("MOBILE", hrMobile);
                        map.put("UID", String.valueOf(userFeedJobPO.hrId));
                        if (!StringUtil.isEmpty(hrName) && !StringUtil.isEmpty(uName) && !StringUtil.isEmpty(uShcoolName) && !StringUtil.isEmpty(hrMobile) && !StringUtil.isEmpty(uMajorName)) {
                            map.put("smsUrl", CommonUtil.smsUrlForHr("gallery=" + "guodu" + "&signature=" + "dajie" + "&ch=djgkdashan" + TimeUtil.getST() + XUESHENG + "&st=" + TimeUtil.getST() + "&sms_uid=" + userFeedJobPO.hrId));
                            // result = SendSms.sendSms(TYPE, XUESHENG, map);
                            result = sendSms.sendSmsSingle(hrMobile, String.valueOf(XUESHENG), map);

                            if (result.contains("1")) {
                                logger.error("main --> sendSms success hrUid:{} uUid:{} hrMobile:{} tplId:{} result:{} map:{}", userFeedJobPO.hrId, userFeedJobPO.userId, hrMobile, XUESHENG, result, map.toString());
                            } else {
                                logger.error("main --> sendSms fail hrUid:{} identity:{} result:{} map:{}", userFeedJobPO.hrId, identity, result, map.toString());
                            }
                        } else {
                            logger.error("main --> getInfo lost hrUid:{} uUid:{} identity:{} map:{}", userFeedJobPO.hrId, userFeedJobPO.userId, identity, map.toString());
                        }
                        break;
                    //其他身份
                    default:
                        hrName = CommonUtil.getUnameByUid(userFeedJobPO.getHrId());
                        uName = CommonUtil.getUnameByUid(userFeedJobPO.getUserId());
                        hrMobile = CommonUtil.getMobileByUid(userFeedJobPO.getHrId());
                        map.put("hrName", hrName);
                        map.put("uName", uName);
                        map.put("userCount", String.valueOf(userFeedJobPO.feedCount));
                        map.put("MOBILE", hrMobile);
                        map.put("UID", String.valueOf(userFeedJobPO.hrId));
                        if (!StringUtil.isEmpty(hrName) && !StringUtil.isEmpty(uName) && !StringUtil.isEmpty(hrMobile)) {
                            map.put("smsUrl", CommonUtil.smsUrlForHr("gallery=" + "guodu" + "&signature=" + "dajie" + "&ch=djgkdashan" + TimeUtil.getST() + OTHER + "&st=" + TimeUtil.getST() + "&sms_uid=" + userFeedJobPO.hrId));
                            //   result = SendSms.sendSms(TYPE, OTHER, map);
                            result = sendSms.sendSmsSingle(hrMobile, String.valueOf(OTHER), map);
                            if (result.contains("1")) {
                                logger.error("main --> sendSms success hrUid:{} uUid:{} hrMobile:{} tplId:{} result:{} map:{}", userFeedJobPO.hrId, userFeedJobPO.userId, hrMobile, OTHER, result, map.toString());
                            } else {
                                logger.error("main --> sendSms fail hrUid:{} identity:{} result:{} map:{}", userFeedJobPO.hrId, identity, result, map.toString());
                            }
                        } else {
                            logger.error("main --> getInfo lost hrUid:{} uUid:{} identity:{} map:{}", userFeedJobPO.hrId, userFeedJobPO.userId, identity, map.toString());
                        }
                }
            } catch (Exception e) {
                logger.error("main --> error hrUid:{} uUid:{} message:{} {}", userFeedJobPO.hrId, userFeedJobPO.userId, e.getMessage(), e);
            }

        }
        userFeedJobForHr.closeConn();
    }
}
